package com.vision.sales.report.cl.ui;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor.Ignore;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.shared.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.CheckBox;
import com.sencha.gxt.widget.core.client.form.SimpleComboBox;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.menu.CheckMenuItem;
import com.sencha.gxt.widget.core.client.menu.Menu;
import com.vision.core.cl.ui.TabItemPanel;
import com.vision.core.cl.ui.component.EnumComboBox;
import com.vision.core.cm.PageOrientation;
import com.vision.core.cm.PageSize;
import com.vision.sales.cl.SalesTasks;
import com.vision.sales.cl.resource.SalesResources;
import com.vision.sales.cm.data.ReportPeriodType;

/**
 * 
 * @author Mark
 *
 */
public class MonthlyGroupSalesReportPanel extends TabItemPanel {

	interface Binder extends UiBinder<Widget, MonthlyGroupSalesReportPanel> { }

	private static final Binder binder = GWT.create(Binder.class);
	
	@UiField VerticalLayoutContainer mainPanel;
	
	@UiField TextButton printBtn;
	
	@UiField TextButton sizeBtn;
	@UiField Menu sizeBtnMenu;
	@UiField CheckMenuItem letterMenuItem;
	@UiField CheckMenuItem legalMenuItem;
	@UiField CheckMenuItem a4MenuItem;
	
	@UiField TextButton orientationBtn;
	@UiField Menu orientationBtnMenu;
	@UiField CheckMenuItem portraitMenuItem;
	@UiField CheckMenuItem landscapeMenuItem;
	
	@UiField TextField title;
	@UiField SimpleComboBox<ReportPeriodType> type;
	@UiField TextField year;
	@UiField CheckBox all;
	@UiField CheckBox jan;
	@UiField CheckBox feb;
	@UiField CheckBox mar;
	@UiField CheckBox apr;
	@UiField CheckBox may;
	@UiField CheckBox jun;
	@UiField CheckBox jul;
	@UiField CheckBox aug;
	@UiField CheckBox sep;
	@UiField CheckBox oct;
	@UiField CheckBox nov;
	@UiField CheckBox dec;
	//@ UiField CheckBox showZeroes;
	
	CheckBox[] allMonths;
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@SuppressWarnings("deprecation")
	public MonthlyGroupSalesReportPanel() {
		setWidget(binder.createAndBindUi(this));

		letterMenuItem.setChecked(true);
		portraitMenuItem.setChecked(true);
		
		title.setValue("Monthly Group Sales");
		title.addValidator(Validators.TITLE_VALIDATOR);
		
		allMonths = new CheckBox[]{ jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec };
		
		//showZeroes.setValue(true);

		Date now = new Date();
		DateTimeFormat fmt = DateTimeFormat.getFormat("yyyy");
		year.setValue(fmt.format(now));

		type.setValue(ReportPeriodType.MONTHLY);
		checkCurrentMonth(now.getMonth());
		
		all.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				boolean value = event.getValue();
				for (CheckBox checkBox : allMonths) {
					checkBox.setValue(value);
				}
			}
		});
		
		ValueChangeHandler<Boolean> monthCheckHandler = new ValueChangeHandler<Boolean>() {
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				boolean value = event.getValue();
				if (!value)
				{
					all.setValue(false);
					return;
				}
				
				for (CheckBox checkBox : allMonths) {
					if (!checkBox.getValue())
					{
						all.setValue(false);
						return;
					}
				}
				all.setValue(true);
			}
		};
		
		for (CheckBox checkBox : allMonths) {
			checkBox.addValueChangeHandler(monthCheckHandler);
		}
	}
		
	// UiFactory Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	@Ignore
	@UiFactory
	SimpleComboBox<ReportPeriodType> initializeReportPaymentTypeCombo() {
		EnumComboBox<ReportPeriodType> cb = new EnumComboBox<ReportPeriodType>(ReportPeriodType.values());
		cb.addSelectionHandler(new SelectionHandler<ReportPeriodType>() {
			@Override
			public void onSelection(SelectionEvent<ReportPeriodType> e) {
				boolean isMonthly = e.getSelectedItem() == ReportPeriodType.MONTHLY;
				all.setEnabled(isMonthly);
				for (CheckBox checkBox : allMonths) {
					checkBox.setEnabled(isMonthly);
					if (!isMonthly)
					{
						checkBox.setValue(false);
					}
				}
			}
		});
		return cb;
	}
	
	// UiHandler Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@UiHandler("printBtn")
	void printClicked(SelectEvent e) {
		// TODO Add checking
		StringBuilder params = new StringBuilder();
		params.append("orientation=" + getOrientation());
		params.append("&size=" + getSize());
		params.append("&title=" + title.getValue());
		params.append("&year=" + year.getValue());
		if (type.getValue() == ReportPeriodType.MONTHLY)
		{
			params.append("&type=M");
			params.append("&months=" + getMonths());
		}
		else if (type.getValue() == ReportPeriodType.ANNUAL)
		{
			params.append("&type=A");
		}	
		Window.open("print/monthly_group_sales?" + params.toString(), null, null);
	}
	
	// ITabItemPanel Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	private void checkCurrentMonth(int month) {
		allMonths[month].setValue(true);
	}

	private String getMonths() {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < allMonths.length; i++) {
			CheckBox checkBox = allMonths[i];
			if (checkBox.getValue()) {
				if (sb.length() > 0)
				{
					sb.append(",");
				}
				sb.append(i + 1);
			}
		}
		return sb.toString();
	}

	@Override
	public String getPanelId() {
		return SalesTasks.MONTHLY_GROUP_SALES.getId();
	}

	@Override
	public TabItemConfig getConfig() {
		if (config == null) {
			config = new TabItemConfig("Monthly Group Sales", true);
			config.setIcon(SalesResources.INSTANCE.monthlyGroupSales());
		}
		return config;
	}


	@Override
	public void loadData() {
		
	}
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	PageSize getSize() {
		if (a4MenuItem.isChecked())
			return PageSize.A4;
		if (legalMenuItem.isChecked())
			return PageSize.LEGAL;
		return PageSize.LETTER;
	}
	
	PageOrientation getOrientation() {
		return portraitMenuItem.isChecked() ? PageOrientation.PORTRAIT : PageOrientation.LANDSCAPE;
	}

}
